<?php
/**
 * Created by PhpStorm.
 * User: luckystar_D
 * Date: 2017/1/21
 * Time: 上午11:44
 */

namespace api\modules\v1\models;

use linslin\yii2\curl\Curl;

/*
* 51充充流量充值接口
*
* 订单状态 $status  OrderProcessing:充值中;
*                  OrderSuccess:充值成功;
*                  OrderFail:充值失败;
*
* 接口状态 $code     Success:提交成功;
*                   InsufficientBalance:余额不足;
*                   SystemError:系统错误;
*                   AccountLimit:账户限制;
*                   DisableAccess:权限不足;
*                   GoodsLimit:商品下架;
*                   OtherError:其他类型错误;
*                   OrderExist:商户订单号已存在;
*                   ParameterError:参数错误;
*                   SignError:签名错误;
*                   OrderNotFind:订单不存在;
* 接口类型:POST;
* 接口参数:@$param = array(
                'MemberId' => self::$memberid,//商户编号
                'Hmac' => $sign,//签名
                'MemberOrderId' => $post['out_order_id'],//商户请求号(外部订单号)
                'GoodsId' => $goodsid['no'],//s商品编号(录入数据库提取)
                'Account' => $post['phone_no'], //充值账号(手机号)
                'Data' => date('YmdHmsB',time()),
                'Version' => '0.1', //版本号
            );
*/
class ChongChong51
{
    public static $secretkey = 'D16B859B25C3B38F1BFABEE1837EA520';
    public static $memberid = 'M0445';
    public static $submit_url = 'http://api.51cc.38.zonghejiaofei.net/api/FlowOrder/Recharge';
    public static $blance_url = 'http://api.51cc.38.zonghejiaofei.net/api/Member/BalanceQuery';
    public static $query_url = 'http://api.51cc.38.zonghejiaofei.net/api/FlowOrder/ReChargeQuery';

    public static function submit($post)
    {
        $goodsid = Site::GetFlowValNo($post);
        $sign = MD5(
            'Account' . $post['phone_no'] .
            'GoodsId' . $goodsid['no'] .
            'MemberId' . self::$memberid .
            'MemberOrderId' . $post['out_order_id'] .
            'Secretkey' . self::$secretkey
        );
        $param = array(
            'MemberId' => self::$memberid,
            'Hmac' => $sign,
            'MemberOrderId' => $post['out_order_id'],
            'GoodsId' => $goodsid['no'],
            'Account' => $post['phone_no'],
            'Data' => date('YmdHmsB', time()),
            'Version' => '0.1',
        );
        $return = self::curl_http(self::$submit_url, $param);
        $return_array = json_decode($return, true);
        $data_array = array(
            'out_order_id' => $post['out_order_id'],
            'flow_val' => $post['flow_val'],
            'phone_no' => $post['phone_no'],
            'order_id' => !empty($return_array['Data']['OrderId']) ? $return_array['Data']['OrderId'] : '',
        );
        return self::returncode($return_array['Result'], $data_array);
    }
    //余额查询接口
    public static function blance()
    {
        $sign = MD5(
            'MemberId' . self::$memberid .
            'Secretkey' . self::$secretkey
        );
        $param = array(
            'MemberId' => self::$memberid,
            'Hmac' => $sign,
            'Data' => date('YmdHmsB', time()),
        );
        $return = self::curl_http(self::$blance_url, $param);
        $return_array = json_decode($return, true);
        return self::returncode($return_array['Result'], array('blance' => $return_array['Data']['Balance']));
    }
    //订单查询接口
    public function query($post)
    {
        $sign = MD5(
            'MemberId' . self::$memberid .
            'MemberOrderId' . $post['out_order_id'] .
            'Secretkey' . self::$secretkey
        );
        $param = array(
            'MemberId' => self::$memberid,
            'Hmac' => $sign,
            'MemberOrderId' => $post['out_order_id'],
            'Data' => date('YmdHmsB',time()),
        );
        $return = self::curl_http(self::$query_url, $param);
        $return_array = json_decode($return, true);
        if ($return_array['Result'] == 'Success') {
            return self::querycode($return_array['Data']['OrderState']);
        } else {
            return self::querycode($return_array['Result']);
        }

        /*switch ($return_array['Result']) {
            case 'Success':
                $result['result'] = 0;//操作成功
                $result['status'] = $return_array['Data']['OrderState'];//订单状态
                $result['phone_no'] = $return_array['Data']['Account'];//手机号码
                $result['out_order_id'] = $return_array['Data']['MemberOrderId'];//外部订单id
                $result['order_id'] = $return_array['Data']['OrderId'];//内部订单id
                $result['err_desc'] = $return_array['Message'];//内部订单id
                break;
            case 'SignError':
                $result['result'] = -3;//签名错误
                break;
            case 'ParameterError':
                $result['result'] = -1;//参数错误
                break;
            case 'SystemError':
                $result['result'] = -4;//系统错误
                break;
            case 'OrderNotFind':
                $result['result'] = -6;//订单不存在
                break;
            case 'OtherError':
                $result['result'] = -7;//其他类型错误
                break;
            default:
                $result['result'] = -7;//其他类型错误
                break;
        }
        return $result;*/
    }
    //回调地址接口
    public function back()
    {
        /*
         *
         */
    }
    //curl_http
    public static function curl_http($url,$param){
        $curl = new Curl();
        $response = $curl->setOption(
            CURLOPT_POSTFIELDS,
            http_build_query($param)
        )->post($url);
        return $response;
    }
    public static function querycode($code = 'OrderProcessing', $data = array())
    {
        switch ($code)
        {

            case 'OrderProcessing' :
                $returncode = 2;
                $message = '充值中';
                break;
            case 'OrderSuccess' :
                $returncode = 3;
                $message = '充值成功';
                break;
            case 'OrderFail' :
                $returncode = 4;
                $message = '充值失败';
                break;
            case 'OrderNotFind':
                $returncode = 5;
                $message = '订单不存在';
                break;
            case 'OtherError':
                $returncode = 5;
                $message = '其他类型错误';
                break;
            case 'SystemError':
                $returncode = 5;
                $message = '系统错误';
                break;
            case 'ParameterError':
                $returncode = 5;
                $message = '参数错误';
                break;
            case 'SignError':
                $returncode = 5;
                $message = '签名错误';
                break;
            default :
                $returncode = 2;
                $message = '充值中';
                break;
        }
        return json_encode([
            'code' => $returncode,
            'message' => $message,
            'data' => $data
        ], JSON_UNESCAPED_UNICODE);
        //Yii::$app->end();
    }
    public static function returncode($code = 'Success', $data = array())
    {
        switch ($code)
        {
            case 'Success' :
                $returncode = 0;
                $message = '处理成功';
                break;
            case 'InsufficientBalance' :
                $returncode = -6;
                $message = '接口账户余额不足';
                break;
            case 'SystemError' :
                $returncode = -7;
                $message = '系统异常';
                break;
            case 'AccountLimit' :
                $returncode = -7;
                $message = '系统异常';
                break;
            case 'DisableAccess' :
                $returncode = -7;
                $message = '系统异常';
                break;
            case 'GoodsLimit' :
                $returncode = -8;
                $message = '通道异常';
                break;
            case 'OtherError' :
                $returncode = -7;
                $message = '系统异常';
                break;
            case 'OrderExist' :
                $returncode = -1;
                $message = '参数错误';
                break;
            case 'ParameterError' :
                $returncode = -1;
                $message = '参数错误';
                break;
            case 'SignError' :
                $returncode = -3;
                $message = '鉴权错误';
                break;
            case 'OrderNotFind' :
                $returncode = -1;
                $message = '参数错误';
                break;
            /*case 'OrderProcessing' :
                $returncode = -7;
                $message = '系统异常';
                break;
            case 'OrderSuccess' :
                $returncode = -7;
                $message = '系统异常';
                break;
            case 'OrderFail' :
                $returncode = -7;
                $message = '系统异常';
                break;*/
            default :
                $returncode = 0;
                $message = '处理成功';
                break;
        }
        return json_encode([
            'code' => $returncode,
            'message' => $message,
            'data' => $data
        ], JSON_UNESCAPED_UNICODE);
        //Yii::$app->end();
    }

}